<template>
  <div class="app-container">
    <el-form class="base-search-form" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="来源渠道" prop="channelCode">
        <el-select v-model="queryParams.channelCode" placeholder="请选择来源渠道" clearable>
          <el-option
            v-for="dict in channelList"
            :key="dict.channelCode"
            :label="dict.channelName"
            :value="dict.channelCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户编号" prop="platformUserId">
        <el-input
          v-model="queryParams.platformUserId"
          placeholder="请输入用户编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      
      <el-form-item label="订单状态" prop="state">
        <el-select clearable v-model="queryParams.state" placeholder="请选择订单状态">
          <el-option
            v-for="item in recharge_order_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="订单编号" prop="outTradeNo">
        <el-input
          v-model="queryParams.outTradeNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付方式" prop="payChannel">
        <el-select clearable v-model="queryParams.payChannel" placeholder="请选择支付方式">
          <el-option
            v-for="item in plat_order_pay_channel"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="充值时间" prop="createTime">
        <el-date-picker clearable
          v-model="rechargeDate"
          type="daterange"
          value-format="YYYY-MM-DD"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          @change="confirmRechargeDate">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="到账时间" prop="receiveTime">
        <el-date-picker clearable
          v-model="receiveDate"
          type="daterange"
          value-format="YYYY-MM-DD"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          @change="confirmReceiveDate">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="base-container mb-10px flex items-center">
      <div class="total">
        <span>累计充值金额：</span>
        <span class="amount">{{ formatNumberWithCommas(formatAmount(summary.recharge_fee)) }}</span>
        <span>元</span>
      </div>
      <div class="total">
        <span>累计优惠金额：</span>
        <span class="amount">{{ formatNumberWithCommas(formatAmount(summary.coupon_fee)) }}</span>
        <span>元</span>
      </div>
      <div class="total">
        <span>累计充值订单数：</span>
        <span class="amount">{{ formatNumberWithCommas(summary.order_sum) }}</span>
        <span>笔</span>
      </div>
    </div>

    <div class="base-container mb-50px">
      <el-row :gutter="10" class="mb-10px">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['platuser:record:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table class="fixed-scrollbar" v-horizontal-scroll border v-loading="loading" :data="rechargeList">
        <el-table-column label="来源渠道" align="center" prop="channelCode" width="130" show-overflow-tooltip />
        <el-table-column label="用户编号" align="center" prop="platformUserId" width="150" show-overflow-tooltip />
        <el-table-column label="手机号码" align="center" prop="mobile" width="120" />
        <el-table-column label="电子卡号" align="center" prop="cardNo" width="150" show-overflow-tooltip />
        <!-- <el-table-column label="卡类型" align="center" prop="cardType" /> -->
        <el-table-column label="支付方式" align="center" prop="payChannel" width="100">
          <template #default="scope">
            <dict-tag :options="plat_order_pay_channel" :value="scope.row.payChannel" />
          </template>
        </el-table-column>
        <el-table-column label="充值金额（元）" align="center" prop="rechargeFee" width="120">
          <template #default="scope">
            <span>{{ formatAmount(scope.row.rechargeFee) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="优惠金额（元）" align="center" prop="couponFee" width="120">
          <template #default="scope">
            <span>{{ formatAmount(scope.row.couponFee) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="余额（元）" align="center" prop="balance" width="100">
          <template #default="scope">
            <span>{{ formatAmount(scope.row.balance) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="订单状态" align="center" prop="state">
          <template #default="scope">
            <dict-tag :options="recharge_order_status" :value="scope.row.state" />
          </template>
        </el-table-column>
        <el-table-column label="充值时间" align="center" prop="rechargeTime" width="180" />
        <el-table-column label="到账时间" align="center" prop="receiveTime" width="180" />
        <el-table-column label="订单编号" align="center" prop="outTradeNo" width="180" show-overflow-tooltip />
      </el-table>
    </div>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Recharge">
import { listRecharge, getRecharge } from "@/api/order/recharge"
import { listChannel } from "@/api/channel/platchannelinfo"

const { proxy } = getCurrentInstance()
const { recharge_order_status, electronic_card_type, plat_order_pay_channel } = proxy.useDict('recharge_order_status', 'electronic_card_type', 'plat_order_pay_channel')

const rechargeList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)
const summary = ref({})
const channelList = ref([])
const rechargeDate = ref([])
const receiveDate = ref([])

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    platformUserId: null,
    mobile: null,
    state: null,
    outTradeNo: null,
    channelCode: null,
    payChannel: null,
    rechargeBeginTime: null,
    rechargeEndTime: null,
    receiveBeginTime: null,
    receiveEndTime: null,
  }
})

const { queryParams } = toRefs(data)

const confirmRechargeDate = (e) => {
  if (e && e.length) {
    queryParams.value.rechargeBeginTime = e[0]
    queryParams.value.rechargeEndTime = e[1]
  } else {
    queryParams.value.rechargeBeginTime = null
    queryParams.value.rechargeEndTime = null
  }
  handleQuery()
}

const confirmReceiveDate = (e) => {
  if (e && e.length) {
    queryParams.value.receiveBeginTime = e[0]
    queryParams.value.receiveEndTime = e[1]
  } else {
    queryParams.value.receiveBeginTime = null
    queryParams.value.receiveEndTime = null
  }
  handleQuery()
}

/** 查询渠道列表 */
function getchannelList() {
  const params = {
    pageNum: 1,
    pageSize: 100
  }
  listChannel(params).then(response => {
    channelList.value = response.rows
  })
}

/** 查询充值记录列表 */
function getList() {
  loading.value = true
  let params = {
    ...queryParams.value,
    'params[rechargeBeginTime]': queryParams.value.rechargeBeginTime,
    'params[rechargeEndTime]': queryParams.value.rechargeEndTime,
    'params[receiveBeginTime]': queryParams.value.receiveBeginTime,
    'params[receiveEndTime]': queryParams.value.receiveEndTime
  }
  delete params.rechargeBeginTime
  delete params.rechargeEndTime
  delete params.receiveBeginTime
  delete params.receiveEndTime
  listRecharge(params).then(response => {
    rechargeList.value = response.rows
    total.value = response.total
    loading.value = false
    summary.value = response.remark || {}
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  rechargeDate.value = []
  receiveDate.value = []
  queryParams.value.rechargeBeginTime = null
  queryParams.value.rechargeEndTime = null
  queryParams.value.receiveBeginTime = null
  queryParams.value.receiveEndTime = null
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('platuser/record/export', {
    ...queryParams.value
  }, `充值订单_${proxy.getFileImportName()}.xlsx`)
}

getchannelList()
getList()
</script>

<style lang="scss" scoped>
.total {
  width: 33.33%;
  color: #606266;
  font-size: 14px;
  padding: 8px 0;

  .amount {
    color: #333;
    font-size: 18px;
    padding-right: 3px;
    font-weight: bold;
  }
}
</style>
